<?php

class Skybest extends CI_Controller {
	public function index(){
		//echo "HERE";
		$this->view();
	}

	public function view()
	{
		$page =  'home';
		if(isset($_REQUEST['page']))
		$page = $_REQUEST['page'];

		//for contact Us Form
		if(isset($_REQUEST['sendcontactusform'])){
			$emailMessageTemplate = "contactus";
			
			$details = $_REQUEST;
			//for contct us form
			$details['receipients'] = array('anthonyteoxon@gmail.com' , 'anthonyteoxon@yahoo.com' , $_REQUEST['email']);
			
			$this->sendEmail($emailMessageTemplate,  $details ,   "New Contact Us Message");
		}
		if($page == 'account')
		{


			if(isset($_REQUEST['id'])){
				$this->updateClientAccount();
				$data['successMessage'] ="Record was successfully Updated.";

				$k = $this->db->query("select * from users where id = ".$_REQUEST['id']." and valid= 1 limit 1" );

				$data['user'] = $k->row();
			}

			if(isset($_REQUEST['editId'])){
				$k = $this->db->query("select * from users where id = ".$_REQUEST['editId']." and valid= 1 limit 1" );
				//$row =
				$data['user'] = $k->row();
			}

		}
			
		$data['page'] = $page; // Capitalize the first letter
			
		$pageData =  $this->db->query("select * from pages where name = '$page' limit 1");
		$data['selectedPagesInMenu'] = $this->db->query("select * from pages where menu_id != 0 order by menu_id");
			
		$data['mastheadImage'] = 'masthead.jpg';
			
		$row = $pageData->row();
			
		if($row != null && $row->masthead != "" && file_exists("../assets/masthead/".$row->masthead))
		$data['mastheadImage'] = $row->masthead;
			
		$data['pageExists'] = false;
			
			
		if($pageData->num_rows() > 0 ){
			$data['pageExists'] = true;
			$data['content'] = $row->content;
			$data['title'] = $row->title;
			//echo  $row->pagesidenav."    ggggggggggggggggg";
			if($row->pagesidenav > 0){
				$sideNav =  $this->db->query("select * from pages where id = ".$row->pagesidenav." limit 1");

				if($sideNav->num_rows() > 0 ){
					$data['sideNav'] = $sideNav->row();
				}
			}


		}
		if($this->session->userdata('client_id') != null)
		$data['transactionList'] = $this->transactionListByUser($this->session->userdata('client_id') , null , null);
		$newsListLimit = " limit 5";

		if($page == 'news')

		$newsListLimit =  "";

		$data['newsList'] = $this->db->query("SELECT * FROM pages WHERE parentid=(SELECT ID FROM PAGES WHERE NAME = 'news' LIMIT 1) $newsListLimit");

		if($page == 'home')

		$data =  $this->loadHomeContents($data);

		if($page == 'logout'){
			$this->logoutCurrentUser();
			$page = 'login';
		}
			
			
		if($page == 'schedule')
		$data['uploadedFiles'] = $this->db->query("select * from uploaded_files where valid = 1" );

		if($page == 'transactionDetails' || $page == 'printTransactionDetails'){
			$data['editId'] = $_REQUEST['editId'];
			$query = $this->getTransaction($_REQUEST['editId']);
			$data['transactionDetails'] = $query->row();
		}
		
		$this->loadClientPage($page , $data);

	}

	function loadHomeContents($data){
		$newsListLimit = "limit 1";
	 				$data['aboutus'] = $this->db->query("SELECT * FROM PAGES WHERE NAME = 'aboutus' LIMIT 1 ")->row();
	 				$data['services'] = $this->db->query("SELECT * FROM PAGES WHERE NAME = 'services' LIMIT 1 ")->row();
	 				$data['contactus'] = $this->db->query("SELECT * FROM PAGES WHERE NAME = 'contactus' LIMIT 1 ")->row();
	 				return $data;

	}

	function updateClientAccount(){
			
		$firstname = $_REQUEST['firstname'];
		$lastname =  $_REQUEST['lastname'];
		$email =  $_REQUEST['email'];
		$middlename =  $_REQUEST['middlename'];
		//	$warehouse =  $_REQUEST['warehouse'];
		$warehouse =  '';
		//	$usertype =  $_REQUEST['usertype'];
		$username = $_REQUEST['username'];
		$password = $_REQUEST['password'];
		$address = $_REQUEST['address'];


		$emailfornewuser = false;
		$emailfornewtransaction = false;
		$emailforsuccessfuldelivery = false;

		$type = "";
		$str="";

		$data = "";

			

			

		if(isset( $_REQUEST['id'])){
			$str = "UPDATE users SET firstname='$firstname',lastname='$lastname',username='$username',email='$email',updatedon=NOW(),warehouse='$warehouse',password='$password',middlename='$middlename',address ='$address',type='$type',emailfornewuser = '$emailfornewuser' ,emailfornewtransaction = '$emailfornewtransaction', emailforsuccessfuldelivery = '$emailforsuccessfuldelivery' where id=". $_REQUEST['id'];

			$this->db->query($str);

			$this->clientUpdateLog('USERS' , 'UPDATED BY CLIENT' ,  "Updated By Client where id = ".$_REQUEST['id']);

		}
	}

	function individualreport($id){
		//ECHO
			
		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);

		$activeSheet = $this->excel->getActiveSheet();

		//name the worksheet
		$activeSheet->setTitle('Skybest');
		//set cell A1 content with some text
		//$this->excel->getActiveSheet()->setCellValue('A1', 'This is just some text value');

		//change the font size
		//$activeSheet->getStyle('A1')->getFont()->setSize(10);
		//make the font become bold
		//$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//FOR HEADER ::
		$activeSheet->setCellValue('B1', $this->session->userdata('user_fullname'));
		
		for($atoz=65 ; $atoz < 65 + 26 ;$atoz++)
		{
			//	echo chr($atoz)."----";
			$activeSheet->getColumnDimension(chr($atoz))->setWidth(30);
			if($atoz > 65)
			$activeSheet->getStyle(chr($atoz))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

		}


		

		$activeSheet->setCellValue('A17', 'Booking Confirmation');
		$activeSheet->setCellValue('A18', 'Check ATW received');
		$activeSheet->setCellValue('A19', 'Forward ATW to Shipper');
		$activeSheet->setCellValue('A20', 'Update/Monitor Shipper');
		$activeSheet->setCellValue('A21', 'Follow-up final SI to Shipper');
		$activeSheet->setCellValue('A22', 'Received final SI from Shipper');
		$activeSheet->setCellValue('A23', 'Send Final pro-forma to Carrier');
		$activeSheet->setCellValue('A24', 'Received B/L Copy from Carrier');
		$activeSheet->setCellValue('A25', 'Encode Billing in BCIS');
		$activeSheet->setCellValue('A26', 'Request for Telex Release');
		$activeSheet->setCellValue('A27', 'Request Payment fo Carrier');
		$activeSheet->setCellValue('A28', 'Revised B/L');
		$activeSheet->setCellValue('A29', 'COMMENT');




/*
		$exportReportItems =  $this->db->query("select * from exportreportitem where reportsId = $id and  valid = 1");
			
		$numberColumn = 66;

		forEach($exportReportItems->result() as $reportItem){

			$column = chr($numberColumn);

			$activeSheet->setCellValue($column.'6', $reportItem->shipper);
			$activeSheet->getStyle($column.'6')->getFont()->setBold(true);
			$activeSheet->setCellValue($column.'7', $reportItem->volume);
			$activeSheet->setCellValue($column.'8', $reportItem->commodity);
			$activeSheet->setCellValue($column.'9', $reportItem->destination);
			$activeSheet->setCellValue($column.'10', $reportItem->vessel);
			$activeSheet->setCellValue($column.'11', $reportItem->carrier);
			$activeSheet->setCellValue($column.'12', $reportItem->lct);
			$activeSheet->setCellValue($column.'13', $reportItem->atd);


			$activeSheet->setCellValue($column.'16', 'Date Completed');
			$activeSheet->getStyle($column.'16')->getFont()->setBold(true);
			$activeSheet->getStyle($column.'16')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);



			$activeSheet->setCellValue($column.'17', $reportItem->status1);
			$activeSheet->setCellValue($column.'18', $reportItem->status2);
			$activeSheet->setCellValue($column.'19', $reportItem->status3);
			$activeSheet->setCellValue($column.'20',$reportItem->status4);
			$activeSheet->setCellValue($column.'21', $reportItem->status5);
			$activeSheet->setCellValue($column.'22', $reportItem->status6);
			$activeSheet->setCellValue($column.'23', $reportItem->status7);
			$activeSheet->setCellValue($column.'24', $reportItem->status8);
			$activeSheet->setCellValue($column.'25', $reportItem->status9);
			$activeSheet->setCellValue($column.'26', $reportItem->status10);
			$activeSheet->setCellValue($column.'27', $reportItem->status11);
			$activeSheet->setCellValue($column.'28', $reportItem->status12);
			$activeSheet->setCellValue($column.'29', $reportItem->comment);

			$numberColumn++;

		}
		*/

		//set aligment to center for that merged cell (A1 to D1)
		//$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
		$filename='skybest.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
			
			
	}
	


}
